package org.zfes.snowier.cloud.zbss.loan.config.dic.service.impl;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zfes.snowier.cloud.zbss.loan.config.dic.dao.DicSysconfigMapper;
import org.zfes.snowier.cloud.zbss.loan.config.dic.model.DicSysconfig;
import org.zfes.snowier.cloud.zbss.loan.config.dic.service.IDicSysconfigService;
import org.zfes.snowier.cloud.zbss.loan.util.CloudStockConst;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.core.idcenter.IDGenerator;
import org.zfes.snowier.core.util.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

//import org.zfes.snowy.base.service.BaseService;

@Service
public class DicSysconfigServiceImpl implements IDicSysconfigService {
    @Autowired
    private DicSysconfigMapper dicSysconfigMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void createDicSysconfig(DicSysconfig dicSysconfig) {
        ZBeanUtil.validateBean(dicSysconfig);
        dicSysconfig.setId(IDGenerator.genLongId());
        Optional<DicSysconfig> sysConfig = loadDicSysconfigByConfCode(dicSysconfig.getConfCode());
        if (!ZObjectUtil.isEmpty(sysConfig)) {
            ZAlert.Error("改编码已经存在");
        }
        dicSysconfig.setCreateTime(ZDateUtil.newDate());
        dicSysconfigMapper.insertSelective(dicSysconfig);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<DicSysconfig> loadDicSysconfigById(Long id) {
        if (id != null) {
            return Optional.ofNullable(dicSysconfigMapper.selectByPrimaryKey(id));
        }
        return Optional.empty();

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateDicSysconfig(DicSysconfig dicSysconfig) {
        ZBeanUtil.validateBean(dicSysconfig);
        Optional<DicSysconfig> sysConfig = loadDicSysconfigByConfCode(dicSysconfig.getConfCode());
        if (!ZObjectUtil.isEmpty(sysConfig) && (!dicSysconfig.getId().equals(sysConfig.get().getId()))) {
            ZAlert.Error("改编码已经存在");
        }
        dicSysconfigMapper.updateByPrimaryKeySelective(dicSysconfig);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setDicSysconfigStatus(Long id, Byte status) {
        ZAssert.notNull(id, "请选择要操作的数据");
        ZAssert.notNull(status, "状态参数错误");
        if (Stream.of(new Byte[]{0, 1, 2}).noneMatch(val -> val.equals(status))) {
            ZAlert.serviceLogicalError("状态参数错误");
        }
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public DataSet loadDicSysconfigDataSet(Map<String, Object> params) {
        ParamMap pm = ParamMap.filterParam(params);
        pm.getObj("confName").ifPresent(v -> pm.updateParam("confName", "%" + v + "%"));
        DataSet ds = DataSet.newDS2(dicSysconfigMapper.selectListCount(pm), dicSysconfigMapper.selectMapList(pm));
        return ds;
    }

    //=========================================================================================================================
    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<DicSysconfig> loadDicSysconfigByConfCode(String confCode) {
        if (ZStrUtil.hasText(confCode)) {
            return Optional.ofNullable(dicSysconfigMapper.selectByConfCode(confCode));
        }
        return Optional.empty();
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> loadValueByConfCode(String confCode) {
        if (ZStrUtil.hasText(confCode)) {
            Optional<DicSysconfig> cf = loadDicSysconfigByConfCode(confCode);
            if (cf.isPresent()) {
                return Optional.ofNullable(cf.get().getConfValue());
            }
            return Optional.empty();
        }
        return Optional.empty();
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public List<DicSysconfig> loadByCodeIn(List<String> params) {
        List<DicSysconfig> loadConnection = dicSysconfigMapper.selectByConfCodeIn(params);
        return loadConnection == null ? Lists.newArrayList() : loadConnection;
    }


    private Optional<String> doGetString(String key) {
        return loadValueByConfCode(key);
    }

    private Optional<Integer> doGetInteger(String key) {
        if (ZStrUtil.hasText(key)) {
            Optional<DicSysconfig> cf = loadDicSysconfigByConfCode(key);
            if (cf.isPresent()) {
                String value = cf.get().getConfValue();
                return Optional.ofNullable(Integer.valueOf(value));
            }
            return Optional.empty();
        }
        return Optional.empty();
    }

    private Optional<Long> doGetLong(String key) {
        if (ZStrUtil.hasText(key)) {
            Optional<DicSysconfig> cf = loadDicSysconfigByConfCode(key);
            if (cf.isPresent()) {
                String value = cf.get().getConfValue();
                return Optional.ofNullable(Long.valueOf(value));
            }
            return Optional.empty();
        }
        return Optional.empty();
    }

    private Optional<Double> doGetDouble(String key) {
        if (ZStrUtil.hasText(key)) {
            Optional<DicSysconfig> cf = loadDicSysconfigByConfCode(key);
            if (cf.isPresent()) {
                String value = cf.get().getConfValue();
                return Optional.ofNullable(Double.valueOf(value));
            }
            return Optional.empty();
        }
        return Optional.empty();
    }


    /**
     * sys_domian
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetSysDomian() {
        return doGetString(CloudStockConst.sys_domian);
    }

    /**
     * wx_AppId
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetWxAppId() {
        return doGetString(CloudStockConst.wx_AppId);
    }

    /**
     * wx_MchId
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetWxMchId() {
        return doGetString(CloudStockConst.wx_MchId);
    }

    /**
     * wx_AppSecret
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetWxAppSecret() {
        return doGetString(CloudStockConst.wx_AppSecret);
    }

    /**
     * wx_ApiSecret
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetWxApiSecret() {
        return doGetString(CloudStockConst.wx_ApiSecret);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetAliAccessKeyId() {
        return doGetString(CloudStockConst.ali_accessKeyId);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetAliAccessKeySecret() {
        return doGetString(CloudStockConst.ali_accessKeySecret);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetAliSmsSignName() {
        return doGetString(CloudStockConst.ali_sms_sign_name);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetAliSmsRegistTplCode() {
        return doGetString(CloudStockConst.ali_sms_regist_tpl_code);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetWxpayCourseOrderNotifyUrl() {
        return doGetString(CloudStockConst.wxpay_CourseOrder_notifyUrl);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetWxpayVipOrderNotifyUrl() {
        return doGetString(CloudStockConst.wxpay_VipOrder_notifyUrl);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetWxpayChannelOrderNotifyUrl() {
        return doGetString(CloudStockConst.wxpay_ChannelOrder_notifyUrl);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<Double> doGetBizServiceCourseExternPercentageRate() {

        return doGetDouble(CloudStockConst.biz_course_extern_percentageRate);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<Integer> doGetBizServiceCourseOrderViewCount() {
        return doGetInteger(CloudStockConst.biz_course_order_view_count);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<Integer> doGetBizChannelSharePoints() {
        return doGetInteger(CloudStockConst.biz_channel_share_points);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<Integer> doGetBizMemberIndexSharePoints() {
        return doGetInteger(CloudStockConst.biz_member_index_share_points);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<Long> doGetBizMemberVipPrice() {
        return doGetLong(CloudStockConst.biz_member_vip_price);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<Integer> doGetBizMemberSignPoints() {
        return doGetInteger(CloudStockConst.biz_member_sign_points);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<Integer> doGetBizOrderAutoCloseTime() {
        return doGetInteger(CloudStockConst.biz_order_auto_close_time);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetBizCourseWxPayBody() {
        return doGetString(CloudStockConst.biz_course_wx_pay_body);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetBizChannelWxPayBody() {
        return doGetString(CloudStockConst.biz_channel_wx_pay_body);
    }

    @Transactional(rollbackFor = Exception.class, readOnly = true)
    @Override
    public Optional<String> doGetBizVipWxPayBody() {
        return doGetString(CloudStockConst.biz_vip_wx_pay_body);
    }

}